package com.sc.byahi.user.client;

import java.util.Map;
import java.util.Map.Entry;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextArea;
import com.sc.byahi.user.shared.Notice;

public class AdminView extends Composite implements ICleaner {
	protected FlexTable ft = new FlexTable();
	protected FlexTable emails = new FlexTable();
	private final Button unapproved = new Button("Show Unapproved Profiles");
	private final Button notice = new Button("Publish Notice");
	private final Button uploadURL = new Button("Show Upload URL");
	private final Button createDummyMaleProfiles = new Button("Create Male Profiles");
	private final Button createDummyFemaleProfiles = new Button("Create Female Profiles");
	private final TextArea txtArea = new TextArea();
	private final Button clearMemcache = new Button("Clear memcache");
	private final Button getMemcache = new Button("Get Memcache");
	private final Button deleteProfileOwner = new Button("Delete profile owner");
	private final Button deleteProfile = new Button("Delete profile");
	private final Button fetchUnsentEmails = new Button("Unsent emails");
	private final Button toggleMemcache = new Button("Toggle Memcache");
	private final Button browserCachedData = new Button("Get browser cached data");
	private final Button toggleTestUpload = new Button("Toggle Test Upload");
	public AdminView() {
		initWidget(ft);
		ft.setWidth("100%");
		int r = 0;
		ft.setWidget(r++, 0, unapproved);
		ft.setWidget(r++, 0, clearMemcache);
		ft.setWidget(r++, 0, notice);
		ft.setWidget(r++, 0, uploadURL);
		ft.setWidget(r++, 0, createDummyMaleProfiles);
		ft.setWidget(r++, 0, createDummyFemaleProfiles);
		ft.setWidget(r++, 0, txtArea);
		ft.setWidget(r++, 0, getMemcache);
		ft.setWidget(r++, 0, deleteProfile);
		ft.setWidget(r++, 0, deleteProfileOwner);
		ft.setWidget(r++, 0, fetchUnsentEmails);
		ft.setWidget(r++, 0, emails);
		ft.setWidget(r++, 0, toggleMemcache);
		ft.setWidget(r++, 0, browserCachedData);
		ft.setWidget(r++, 0, toggleTestUpload);
		fetchUnsentEmails.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				emails.clear();
				GreetingService.Util.getInstance().getUnsentEmails(new AsyncCallback<Map<Long, String>>() {

					@Override
					public void onSuccess(Map<Long, String> result) {
						int row = 1;
						for (Entry<Long, String> entry : result.entrySet()) {
							final Long id = entry.getKey();
							String value = entry.getValue();
							emails.setWidget(row, 0, new HTML(id.toString()));
							emails.setWidget(row, 1, new HTML(value));
							final Button send = new Button("Send");
							emails.setWidget(row++, 2, send);
							send.addClickHandler(new ClickHandler() {

								@Override
								public void onClick(ClickEvent event) {
									GreetingService.Util.getInstance().approveEmail(id, new AsyncCallback<Boolean>() {

										@Override
										public void onSuccess(Boolean result) {
											send.setVisible(false);
										}

										@Override
										public void onFailure(Throwable caught) {
											UIUtil.showGenericFailureDialog();
										}
									});

								}
							});
						}

					}

					@Override
					public void onFailure(Throwable caught) {
						UIUtil.showGenericFailureDialog();
					}
				});

			}
		});
		unapproved.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				History.newItem("searchunapproved", true);
			}
		});
		createDummyMaleProfiles.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				GreetingService.Util.getInstance().createTestProfiles("Male", txtArea.getText().trim(), new AsyncCallback<String>() {

					@Override
					public void onSuccess(String result) {
						txtArea.setText(result);
					}

					@Override
					public void onFailure(Throwable caught) {
						UIUtil.showGenericFailureDialog();
					}
				});

			}
		});
		createDummyFemaleProfiles.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				GreetingService.Util.getInstance().createTestProfiles("Female", txtArea.getText().trim(), new AsyncCallback<String>() {

					@Override
					public void onSuccess(String result) {
						txtArea.setText(result);
					}

					@Override
					public void onFailure(Throwable caught) {
						UIUtil.showGenericFailureDialog();

					}
				});

			}
		});
		uploadURL.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				GreetingService.Util.getInstance().getPublicUploadURL(new AsyncCallback<String>() {

					@Override
					public void onSuccess(String result) {
						txtArea.setText(result);
					}

					@Override
					public void onFailure(Throwable caught) {
						UIUtil.showGenericFailureDialog();
					}
				});

			}
		});
		deleteProfileOwner.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				GreetingService.Util.getInstance().deleteProfileOwner(txtArea.getText().trim(), new AsyncCallback<Boolean>() {

					@Override
					public void onSuccess(Boolean result) {
						Window.alert("Profile owner delete status:" + result);
					}

					@Override
					public void onFailure(Throwable caught) {
						UIUtil.showGenericFailureDialog();

					}
				});

			}
		});
		deleteProfile.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				String pid = txtArea.getText().trim();
				GreetingService.Util.getInstance().deleteProfile(Long.parseLong(pid), new AsyncCallback<Boolean>() {

					@Override
					public void onSuccess(Boolean result) {
						Window.alert("Profile delete status:" + result);
					}

					@Override
					public void onFailure(Throwable caught) {
						UIUtil.showGenericFailureDialog();
					}
				});

			}
		});

		clearMemcache.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				GreetingService.Util.getInstance().clearMemcache(new AsyncCallback<Boolean>() {

					@Override
					public void onSuccess(Boolean result) {
						if (result) {
							Window.alert("Memcache cleared.");
						}
					}

					@Override
					public void onFailure(Throwable caught) {
						UIUtil.showGenericFailureDialog();
					}
				});

			}
		});
		toggleMemcache.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				GreetingService.Util.getInstance().toggleMemcache(new AsyncCallback<Boolean>() {

					@Override
					public void onSuccess(Boolean result) {
						Window.alert("Memcache is: " + result);
					}

					@Override
					public void onFailure(Throwable caught) {
						UIUtil.showGenericFailureDialog();
					}
				});

			}
		});
		toggleTestUpload.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				GreetingService.Util.getInstance().toggleTestUpload(new AsyncCallback<Boolean>() {
					
					@Override
					public void onSuccess(Boolean result) {
						Window.alert("Test Upload is: " + result);
					}
					
					@Override
					public void onFailure(Throwable caught) {
						UIUtil.showGenericFailureDialog();
					}
				});
				
			}
		});

		getMemcache.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				GreetingService.Util.getInstance().getMemcache(new AsyncCallback<String>() {

					@Override
					public void onSuccess(String result) {
						byte[] base64 = com.sc.byahi.user.shared.Base64Utils.fromBase64(result);
						txtArea.setText(new String(base64));
					}

					@Override
					public void onFailure(Throwable caught) {
						UIUtil.showGenericFailureDialog();
					}
				});

			}
		});
		notice.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				String text = txtArea.getText();
				if("".equals(text)){
					GreetingService.Util.getInstance().deleteNotice(new AsyncCallback<Void>() {
						@Override
						public void onSuccess(Void result) {
						}

						@Override
						public void onFailure(Throwable caught) {
							UIUtil.showGenericFailureDialog();
						}
					});
				}else{
					Notice notice = new Notice();
					notice.setNoticeText(text);
					GreetingService.Util.getInstance().publishNotice(notice, new AsyncCallback<Void>() {
						@Override
						public void onSuccess(Void result) {
							
						}
						
						@Override
						public void onFailure(Throwable caught) {
							UIUtil.showGenericFailureDialog();
						}
					});
				}
			}
		});
		browserCachedData.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				txtArea.setText(AppController.get().getBrowserCachedData());
			}
		});
	}

	@Override
	public void clean() {
		ft.removeAllRows();
	}

}
